package croner

import (
	"apibe/grpc/rome/v1/share/logger"
	"github.com/robfig/cron/v3"
	"time"
)

var c *cron.Cron // 一个统一的任务队列

var task map[string]cron.EntryID

var CurrentHour string // 当前小时
//var CurrentMinute string // 当前分钟

type Options struct {
	TouristInit bool
}

func TaskRemove(key string) {
	c.Remove(task[key])
	delete(task, key)
}

func TaskAddFunc(key string, spec string, cmd func()) {
	if task[key] != 0 { // 如果该定时任务已经存在的话，就不重新创建了
		return
	}
	entryID, err := c.AddFunc(spec, cmd)
	if err != nil {
		logger.Logger.Errorf("cron %v is not allowed err : %v", key, err.Error())
	}
	task[key] = entryID
}

func Init() {
	newTask := make(map[string]cron.EntryID, 100)
	task = newTask
	c = cron.New()
	CurrentHour = time.Now().Format("06010215")
	TaskAddFunc("current_hour", "1 * * * *", func() {
		CurrentHour = time.Now().Add(8 * time.Hour).Format("06010215")
	})
	c.Start()
}
